package com.xxx.video.dynamic.fragments;

import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.xxx.video.R;
import com.xxx.video.commen.adapters.BaseRecycleViewAdapter;
import com.xxx.video.commen.dbs.beans.SearchFriendHistoryBean;
import com.xxx.video.commen.dbs.utils.DbUtils;
import com.xxx.video.commen.fragments.BaseFragment;
import com.xxx.video.dynamic.activitys.FindFriendActivity;
import com.xxx.video.dynamic.adapters.FindFriendRecommendAdapter;
import com.xxx.video.dynamic.presenters.FindFriendRecommendPresenter;
import com.xxx.video.dynamic.presenters.IFindFriendRecommendView;

import java.util.List;
import java.util.Objects;

import butterknife.BindView;

public class FindFriendRecommendFragment extends BaseFragment<FindFriendRecommendPresenter> implements IFindFriendRecommendView, BaseRecycleViewAdapter.OnItemClickListener {

    @BindView(R.id.rcv_find_friend_recommend_list)
    RecyclerView rcvFindFriendRecommendList;

    public static FindFriendRecommendFragment newInstance(Bundle args) {
        FindFriendRecommendFragment fragment = new FindFriendRecommendFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_find_friend_recommend;
    }

    @Override
    protected FindFriendRecommendPresenter createPresenter() {
        return new FindFriendRecommendPresenter(Objects.requireNonNull(getActivity()), this);
    }

    @Override
    public void initData(Bundle extras) {
        showLayoutStatus(1);
        getPresenter().getSearchHistoryList();
    }

    @Override
    public void refreshData(Bundle extras) {

    }

    @Override
    public void showRootLayoutStatus(int status) {
        showLayoutStatus(status);
    }

    @Override
    public void initSearchHistoryListData2View(List<SearchFriendHistoryBean> searchFriendHistoryBeanList) {
        rcvFindFriendRecommendList.setLayoutManager(new LinearLayoutManager(getActivity()));
        FindFriendRecommendAdapter findFriendRecommendAdapter = new FindFriendRecommendAdapter(Objects.requireNonNull(getActivity()));
        rcvFindFriendRecommendList.setAdapter(findFriendRecommendAdapter);
        findFriendRecommendAdapter.setSearchHistoryListData(searchFriendHistoryBeanList);
        findFriendRecommendAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(Object object, int position, int type) {
        if (type == 3) {
            try {
                SearchFriendHistoryBean searchFriendHistoryBean = (SearchFriendHistoryBean) object;
                ((FindFriendActivity) getActivity()).search(searchFriendHistoryBean.searchWord);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (type == 4) {
            DbUtils.getInstance().clearAllSearchHistoryListBean();
            getPresenter().getSearchHistoryList();
        }
    }
}
